// pages/change/change.js
var util = require('../../../utils/util.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    inputValue: '',
    pwsValue: "",
    boolean1: true,
    boolean2: true,
    boolean3: true,
    TiaoZhuan:false,
    focus1:false,
    focus2: false,
    focus3: false,
    types:'text'
  },
  clearInput: function () {
    this.setData({ inputValue: '' })
  },
  EventHandle1: function () {
    var bol = this.data.boolean1;
    this.setData({
      boolean1: !bol,
      focus1:true
    })

    console.log(this.data.focus)
  },
  EventHandle2: function () {
    var bol = this.data.boolean2;
    this.setData({
      boolean2: !bol,
      focus2: true
    })
  },
  EventHandle3: function () {
    var bol = this.data.boolean3;
    this.setData({
      boolean3: !bol,
      focus3: true
    })
  },
  goLogin: function () {
    wx.navigateBack({
      url: "../login/login"
    })
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var userid = wx.getStorageSync('UserID') || []
    if (userid == "") { //如果没有获取到userid，就返回我的
      wx.navigateBack({
        delta: 1
      })
    }
    that.GetUserInfo(function (re) {//先判断 是否已经保存过密码
      that.GetUserPwd(re); //判断密码是否为空
    }); 
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {    
    if (this.data.TiaoZhan) {
      wx.showModal({
        title: '提示',
        content: '若绑定成功，请重新进入',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            setTimeout(function () {
              wx.navigateBack({
                delta: 1
              })
            }, 1000);
            
          }
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  getOldPwd: function (e) {    //旧密码
    var oldpwd = e.detail.value;
    this.setData({ OldPwd: "" });
    this.setData({ OldPwd: oldpwd });
  },
  getNewPwd: function (e) {    //监听密码
    var newpwd = e.detail.value;
    this.setData({ NewPwd: "" });
    this.setData({ NewPwd: newpwd });
  },
  getRePwd: function (e) {    //监听确认密码
    var reuserpwd = e.detail.value;
    this.setData({ ReUserPwd: "" });
    this.setData({ ReUserPwd: reuserpwd });
  },
  updatapwd:function(){ //修改密码
    var that = this;
    var userid = wx.getStorageSync('UserID') || []
    var oldpwd = that.data.OldPwd;
    var newpwd = that.data.NewPwd;
    var reuserpwd = that.data.ReUserPwd;

    if(userid!=""){ //UserID 不为空
      if (oldpwd == undefined || oldpwd==""){ 
        util.ShowError("旧密码不能为空");
        return;
      }
      if (newpwd == undefined || newpwd=="") {
        util.ShowError("新密码不能为空");
        return;
      }
      if (reuserpwd == undefined || reuserpwd =="") {
        util.ShowError("确认密码不能为空");
        return;
      }
      if(newpwd != reuserpwd){
        util.ShowError("两次密码不一致");
        return;
      }
      // 验证通过 修改密码
      var data = {
        userid:userid,
        oldpwd: oldpwd,
        newpwd: newpwd
      };
      data.act = "UserUpdatePwd";
      util.Ajax(data, function (json) {
        if (json.state) {
          util.ShowSuccess(json.msg);    
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 1000);
        } else {
          util.ShowError(json.msg);
        }
      });   
    }
  },
  GetUserInfo: function (callback) { //获取用户信息    
    var that = this;
    wx.getStorage({
      key: 'UserID',
      success: function (res) {
        var data = {
          id: res.data
        }
        data.act = "Find_UserByID";
        util.Ajax(data, function (json) {      
          that.setData({userpwd:json.data.UserPwd});          
          callback(json.data.UserPwd);
        });

      }
    })
  },
  GetUserPwd:function(pwd){ //判断密码
    var that =this;    
    if (pwd == ""){
      wx.showModal({
        title: '提示',
        content: '您还没有绑定手机号,点击确定跳转绑定页面。',
        showCancel:false,
        success: function (res) {
          if (res.confirm) {
            that.setData({TiaoZhan:true});
            wx.navigateTo({
              url: '../bound/bound'
            })
          }       
        }
      })

    }
  }
})